package FileProcess;

import DataPreprocess.CleanStemmingWords;
import DataPreprocess.CleanStopWords;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import Result.GetFile;

public class TextRead 
{
	private CleanStopWords cleanStopWords = null;
	private CleanStemmingWords cleanStemmingWords = null;
	private GetFile getfile = new GetFile("result",500);
	private String indexPath = null;
	public TextRead(String indexPath)
	{
		this.cleanStopWords = new CleanStopWords("table/stopword.txt", 27);
		this.cleanStemmingWords = new CleanStemmingWords();
		this.indexPath = indexPath;
	}
	private void _001_judgeDoSearch(String articleId, String content, long tokenNum)
	{
		if(!this.cleanStopWords.ACT001_judgeWordIsStopWord(content))
		{
			content = this.cleanStemmingWords.ACT001_doStemming(content);
			//System.out.println(tokenNum + " " + content);
			//start query
			getfile.ACT_001_SearchAllandWrite(this.indexPath, content, articleId, tokenNum);
		}
	}
	public void ACT001_readAndQuery(String filePath)
	{
		FileReader fr = null;
        BufferedReader br = null;
        String curArticle = "";  //record current reading article id
        String curWord = "";  //record current reading word
        long index = 0, index_inArticle = 0;
	    try 
	    {
	        fr = new FileReader(filePath);
	        br = new BufferedReader(fr);
            String str_readLine = "";
            //read first two line as garbage
            br.readLine();
            br.readLine();
	        while (br.ready()) 
	        {
                str_readLine = br.readLine();
                if(str_readLine.endsWith("\","))  //now reading the content
                {
                	
                	//set current reading word
                	curWord = str_readLine.trim();
                	curWord = curWord.substring(1, curWord.length() - 2);
                	//judge and do search
                	this._001_judgeDoSearch(curArticle, curWord, index_inArticle);
                	//add index in article token number
                	index_inArticle = index_inArticle + 1;
                	index ++;
                	if(index % 10000 == 0)
    	    	    {
                        System.out.println("read and query text " + index);
    	    	    }
                }
                else if(str_readLine.endsWith("\""))
                {
                	//set current reading word
                	curWord = str_readLine.trim();
                	curWord = curWord.substring(1, curWord.length() - 1);
                	//judge and do search
                	this._001_judgeDoSearch(curArticle, curWord, index_inArticle);
                	//add index in article token number
                	index_inArticle = index_inArticle + 1;
                }
                else if(str_readLine.endsWith(": [")) //now reading title of another article
                {
                	//set current reading article
                	curArticle = str_readLine.trim();
                	curArticle = curArticle.substring(1, curArticle.length() - 4);
                	//System.out.println(curArticle);
                	//initial current reading index of content
                	index_inArticle = 0;
                }
	        }
	        //file reading finish, dispose all the resources after using them.
	        fr.close();
	        br.close();
	        this._001_judgeDoSearch(curArticle, curWord, -1);  //-1 represents reading finishes
	    } 
	    catch (FileNotFoundException e) 
	    {
	      e.printStackTrace();
	    } 
	    catch (IOException e) 
	    {
	      e.printStackTrace();
	    }
	}
	public static void main(String[] args) 
	{
		new TextRead("B").ACT001_readAndQuery("myText.json");
	}
}